/**
 * 数据库信息配置相关JS
 */

$(function () {
    // 初始化表格
    var dg = $('#dg').datagrid({
        url: basePath + "/rptTools/user/queryUserInfo",
        singleSelect: true,
        rownumbers:true,
        striped: true,
        remoteSort: true,
        pagination:true,//显示分页
        pageSize: 20,
        columns:[
            [
                {field:'userId',title:'用户ID',width:320},
                {field:'userName',title:'用户名称',width:350},
                {field:'operate',title:'操作',width:350,align:'center',formatter:function(value,row,index){
                    var $row = JSON.stringify(row).replace(/\"/g,"'");
                    return '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="userCfg.editUser(' +  $row + ')" href="#">修改配置</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:10px;" onClick="userCfg.delUser(\'' +  row.userId + '\',\'' + row.userName +'\'' + ')" href="#">删除配置</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:35px;" onClick="userCfg.resetPassword(\'' +  row.userId + '\'' + ')" href="#">重置密码</a>'
                }}
            ]
        ]
    });
});

var userCfg = {
    /**
     * 查询数据
     */
    query : function() {
        var userName = $('#userName').val();

        var param = {};
        param.userName = userName;
        $('#dg').datagrid({
            url:basePath + "/rptTools/user/queryUserInfo",
            queryParams:param
        });
    },
    addUser : function () {
        this.resetWin();
        $('#addUserWin').window('open');

    },
    editUser : function (userInfo) {
        this.resetEditWin();
        var userId = userInfo.userId;
        var userName = userInfo.userName;
        $("#editUserId").textbox('setValue',userId);
        $("#editUserName").textbox('setValue',userName);
        $('#editUserWin').window('open');
    },
    delUser : function (userId, userName) {
        var confirmMsg = '确认删除' + userName + '吗?'
        var func = this;
        $.messager.confirm('提示', confirmMsg, function(r){
            if (r){
                func.showMask();
                $.ajax({
                    url:basePath + "/rptTools/user/deleteUser",
                    type:'POST',
                    dataType:'json',
                    data:{
                        userId: userId
                    },
                    async: false,
                    success:function(data){
                        $("#mask").hide();
                        var status = data.status;
                        var errorMsg = data.errorMsg;
                        if(status == "1") {
                            alert("删除用户成功");
                            $('#dg').datagrid({
                                url:basePath + "/rptTools/user/queryUserInfo"
                            });
                        } else {
                            alert(errorMsg);
                        }
                    }
                })
            }
        });
    },
    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").show();
    },
    saveUser : function () {
        var errorMsg = '';
        var errorFlag = false;
        var userId = $("#instUserId").val();
        var userName = $("#instUserName").val();
        if(userId == '') {
            errorMsg = '用户ID不能为空';
            errorFlag = true;
        } else if(userName == '') {
            errorMsg = '用户名称不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.userId = userId;
        param.userName = userName;
        $.ajax({
            url:basePath + "/rptTools/user/addUserInfo",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("新增用户成功");
                    $('#addUserWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/user/queryUserInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    updateUser : function () {
        var errorMsg = '';
        var errorFlag = false;
        var userId = $("#editUserId").val();
        var userName = $("#editUserName").val();
        if(userName == '') {
            errorMsg = '用户名称不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.userId = userId;
        param.userName = userName;
        $.ajax({
            url:basePath + "/rptTools/user/updateUserInfo",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("用户信息修改成功");
                    $('#editUserWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/user/queryUserInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    resetPassword : function (userId) {
        this.showMask();
        var param = {};
        param.userId = userId;
        $.ajax({
            url:basePath + "/rptTools/user/resetPassword",
            type:'POST',
            dataType:'json',
            data: param,
            async: true,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("重置密码成功");
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    modifyPassword : function () {
        var param = {};
        var oldPasswd = $("#oldPasswd").val();
        var newPasswd = $("#newPasswd").val();
        var confirmPasswd = $("#confirmPasswd").val();
        if(oldPasswd == '') {
            alert("原密码不能为空");
            return;
        }
        if(newPasswd == '') {
            alert("新密码不能为空");
            return;
        }
        if(confirmPasswd == '') {
            alert("确认密码不能为空");
            return;
        }
        param.oldPasswd = oldPasswd;
        param.newPasswd = newPasswd;
        param.confirmPasswd = confirmPasswd;
        this.showMask();
        $.ajax({
            url:basePath + "/rptTools/user/updatePassword",
            type:'POST',
            dataType:'json',
            data: param,
            async: true,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("修改密码成功");
                    parent.location.href = basePath + "/noSession";
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    resetWin : function () {
        $("#instUserId").textbox('setValue','');
        $("#instUserName").textbox('setValue','');
    },
    resetEditWin : function () {
        $("#editUserId").textbox('setValue','');
        $("#editUserName").textbox('setValue','');
    },
    closeWin : function (winId) {
        $('#' + winId).window('close');
    }
}
